package com.sangmo.boot.framework.data.json;

import com.sangmo.boot.framework.common.utils.JsonUtils;
import com.sangmo.boot.framework.data.json.context.ExtViewContext;
import com.sangmo.boot.framework.data.json.meta.ExtViewport;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.core.annotation.AnnotationUtils;

import java.util.Set;

public class ExtViewportHelper {
    public static Object genViewportObject(Object bean) {
        String group = ExtViewContext.get().getGroup();

        Set<ExtViewport> ports = AnnotationUtils.getRepeatableAnnotations(bean.getClass(), ExtViewport.class);
        Class matchedClass = null;
        for (ExtViewport viewport : ports) {
            if (ArrayUtils.contains(viewport.groups(), group)) {
                matchedClass = viewport.value();
                break;
            }
        }

        if (null != matchedClass) {
            return JsonUtils.createMapper().convertValue(bean, matchedClass);
        } else {
            return null;
        }
    }
}
